﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using StartEAS_API.Utils;
using StartsEAS_API.BLL.Interfaces.IWorkerB;
using StartsEAS_API.Model;

namespace StartsEAS_API.Controllers.WorkerContor
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    public class WorkerController : ControllerBase
    {
        private readonly IWorkerBLL _workerBll;
        public WorkerController(IWorkerBLL workerBll) { 
            _workerBll = workerBll;
        }
        /// <summary>
        /// 获取所有职工信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetAllWorker() {
            try
            {
                return _workerBll.GetAllWorker();
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetAllWorker服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 新增职工信息
        /// </summary>
        /// <param name="data">职工信息实体JSON</param>
        /// <returns></returns>
        [HttpPost]
        public ResultUtils InsertWorker(JObject data) {
            try
            {
                return _workerBll.InsertWorker(data);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"InsertWorker服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 修改职工信息
        /// </summary>
        /// <param name="data">职工信息实体JSON</param>
        /// <returns></returns>
        [HttpPut]
        public ResultUtils UpdateWorker(JObject data) {
            try
            {
                return _workerBll.UpdateWorker(data);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"UpdateWorker服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 查询员工信息
        /// </summary>
        /// <param name="workerId">员工编号</param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetWorder(string workerId) {
            try
            {
                return _workerBll.GetWorder(workerId);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetWorder服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
    }
}
